package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.entity.DistrictSysNotice;
import com.gxa.mapper.NoticeMapper;
import com.gxa.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    //分页查询所有公告
    @Override
    public R findNoticeList(Integer currentPage, Integer pageSize) {
        R r = new R();
        try{
            PageHelper.startPage(currentPage,pageSize);
            List<DistrictSysNotice> districtSysNotices = noticeMapper.findNoticeList();
            PageInfo<DistrictSysNotice> pageInfo = new PageInfo<DistrictSysNotice>(districtSysNotices);
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    //按条件分页查询
    @Override
    public R selectByTitle(Integer currentPage, Integer pageSize, String noticeTitle, String createPeople, Date createDate, int status) {
        R r = new R();
        try{
            PageHelper.startPage(currentPage,pageSize);
            List<DistrictSysNotice> districtSysNotices = noticeMapper.selectByTitle(noticeTitle, createPeople, createDate, status);
            PageInfo<DistrictSysNotice> pageInfo = new PageInfo<DistrictSysNotice>(districtSysNotices);
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    //新增
    @Override
    public R addNotice(DistrictSysNotice districtSysNotice) {
        R r = new R();
        int row = noticeMapper.addNotice(districtSysNotice);
        if (row > 0 ){
            r.setCode(200);
            r.setMsg("添加成功");
        }else {
            r.setCode(400);
            r.setMsg("添加失败");
        }
        return r;
    }

    //修改
    @Override
    public R updateNotice(DistrictSysNotice districtSysNotice) {
        R r = new R();
        int row = noticeMapper.updateNotice(districtSysNotice);
        if (row > 0 ){
            r.setCode(200);
            r.setMsg("修改成功");
        }else {
            r.setCode(400);
            r.setMsg("修改失败");
        }
        return r;
    }

    //删除
    @Override
    public R delNotice(int noticeId) {
        R r = new R();
        int row = noticeMapper.delNotice(noticeId);
        if (row > 0 ){
            r.setCode(200);
            r.setMsg("删除成功");
        }else {
            r.setCode(400);
            r.setMsg("删除失败");
        }
        return r;
    }

    //查看详情
    @Override
    public R detailsNotice(int noticeId) {
        R r = new R();
        try{
            List<DistrictSysNotice> districtSysNotices = noticeMapper.detailsNotice(noticeId);
            r.setCode(200);
            r.setMsg("success");
            r.setData(districtSysNotices);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    //发布
    @Override
    public R releaseNotice(int noticeId,Date releaseDate) {
        R r = new R();
        int row = noticeMapper.releaseNotice(noticeId,releaseDate);
        if (row > 0 ){
            r.setCode(200);
            r.setMsg("发布成功");
        }else {
            r.setCode(400);
            r.setMsg("发布失败");
        }
        return r;
    }

    //撤回
    @Override
    public R withdrawNotice(int noticeId) {
        R r = new R();
        int row = noticeMapper.withdrawNotice(noticeId);
        if (row > 0 ){
            r.setCode(200);
            r.setMsg("撤回成功");
        }else {
            r.setCode(400);
            r.setMsg("撤回失败");
        }
        return r;
    }


}
